//
//  CourseCreationViewController.m
//  Gradekeeper0.2
//
//  Created by Tim Qian on 09-11-30.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "CourseCreationViewController.h"
#define LEGAL	@"0123456789"

@implementation CourseCreationViewController

@synthesize semester, nameTextField, numberTextField, creditTextField, typesTextField, weightsTextField, updateButton;

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil semester:(Semester*)newSemester {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.semester = newSemester;
    }
    return self;
}

- (IBAction) update:(id)sender {
	
	if ([[nameTextField text] length] < 1 ||
		[[numberTextField text] length] < 1 ||
		[[creditTextField text] length] < 1 ||
		[[typesTextField text] length] < 1 ||
		[[weightsTextField text] length] <1){
		
		UIAlertView *alert = [[UIAlertView alloc] 
							  initWithTitle:@"Invalid input" 
							  message:@"One or more field is empty, please input." 
							  delegate:self
							  cancelButtonTitle:nil
							  otherButtonTitles:nil]; 
		[alert addButtonWithTitle:@"Ok"];
		[alert show];
		[alert release];
	} else if ([[nameTextField text] length] > 15 ||
			   [[numberTextField text] length] > 3 ||
			   [[creditTextField text] length] > 2) {
		UIAlertView *alert = [[UIAlertView alloc] 
							  initWithTitle:@"Invalid input" 
							  message:@"One or more field exceeds capacity." 
							  delegate:self
							  cancelButtonTitle:nil
							  otherButtonTitles:nil]; 
		[alert addButtonWithTitle:@"Ok"];
		[alert show];
		[alert release];
	} else {
		if (semester != nil) {
			if (![semester addCourse:[[Courses alloc] initWithCatagory:[nameTextField text] 
																number:[[numberTextField text] intValue]
																credit:[[creditTextField text] intValue]
																 types:[typesTextField text]
															   weights:[weightsTextField text]]]) {
				UIAlertView *alert = [[UIAlertView alloc] 
									  initWithTitle:@"Invalid input" 
									  message:@"The course you entered already exists." 
									  delegate:self
									  cancelButtonTitle:nil
									  otherButtonTitles:nil]; 
				[alert addButtonWithTitle:@"Ok"];
				[alert show];
				[alert release];
			} else {
				UIAlertView *alert = [[UIAlertView alloc] 
									  initWithTitle:@"Success" 
									  message:@"New course has been created" 
									  delegate:self
									  cancelButtonTitle:@"Ok" 
									  otherButtonTitles:nil]; 
				[alert show];
				[alert release];
			}
		}

	}
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	//Create a cancel button that lets the user cancel course creation/edit
	self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" 
																	 style:UIBarButtonItemStyleBordered 
																	target:self 
																	action:@selector(cancelCreate:)];
	self.title = [NSString stringWithFormat:@"Course Creation"];
	
	nameTextField.returnKeyType = numberTextField.returnKeyType = creditTextField.returnKeyType = typesTextField.returnKeyType = weightsTextField.returnKeyType = UIReturnKeyDone;
}

- (void)cancelCreate:(id)sender {
	//If user confirms cancellation, current view will be popped and return to SemTableView
	UIAlertView *alert = [[UIAlertView alloc] 
						  initWithTitle:@"Cancel Confirmation" 
						  message:@"Are you sure you would like to cancel the action?" 
						  delegate:self
						  cancelButtonTitle:@"Yes" 
						  otherButtonTitles:nil]; 
	[alert addButtonWithTitle:@"No"];
    [alert show];
	
	[alert release];
} //cancelCreate

/*// allow only legal input ( 3 numbers)
- (BOOL)textField:(UITextField *)textField charSet:(NSString *) set {
	
	NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:set] invertedSet];
	NSString *filtered = [[[textField text] componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
	
	return [string isEqualToString:filtered];
} //textField*/

//If cancellation is confirmed, pop to root view controller
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if ([alertView.title isEqualToString:@"Invalid input"]){
	} else { 
		if (buttonIndex == 0) {
			[[self navigationController] popToRootViewControllerAnimated:YES];
		}
    }
} //alertView

//Hides the keyboard after finishing text input (clicking done)
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	[textField resignFirstResponder];
	return YES;
} //textFieldShouldReturn
									  
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
